package com.zros.modules.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zros.common.api.vo.PageResult;
import com.zros.common.api.vo.Result;
import com.zros.common.exception.JeecgBootException;
import com.zros.common.util.fastDFS.FastDFSUpUtil;
import com.zros.modules.business.entity.Shop;
import com.zros.modules.business.entity.UserApply;
import com.zros.modules.business.service.IShopService;
import com.zros.modules.business.service.IUserApplyService;
import com.zros.modules.business.vo.ShopVo;
import com.zros.modules.business.vo.UserApplyVo;
import com.zros.modules.system.dto.UserInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvkai
 * @since 2020-04-18
 */
@RestController
@RequestMapping("/business/user-apply")
public class UserApplyController {

    @Autowired
    private IUserApplyService userApplyService;

    /**
     * 分页查询渠道
     */
    @ApiOperation(value = "分页查询商铺")
    @RequiresPermissions("business:user-apply:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PageResult<UserApplyVo> queryPageList(HttpServletRequest request, Page<UserApplyVo> page, UserApply userApply)
    {
        //获取当前用户
        UserInfo currentUser = new UserInfo();

        Page<UserApplyVo> pageUserApply = userApplyService.queryList(page, userApply);
        PageResult<UserApplyVo> pageResult = new PageResult<UserApplyVo>(pageUserApply.getTotal(), pageUserApply.getRecords());
        return pageResult;

    }

    /**
     * 账号申请
     */
    @ApiOperation(value = "添加商铺")
    @RequiresPermissions("business:user-apply:list")
    @PostMapping("/create")
    public Result<?> create(@RequestBody UserApply userApply)
    {
        QueryWrapper<UserApply> ew = new QueryWrapper<>();
        ew.eq("card_num", userApply.getCardNum());
        List<UserApply> roleList = userApplyService.list(ew);
        if (!CollectionUtils.isEmpty(roleList)) {
            throw new JeecgBootException("此身份证号码已经申请过账号！");
        }
        //图片上传
        String img = userApply.getImg();
        String[] arr = img.split(";base64,");
        String extensionName = arr[0].replaceAll("data:image/", ".");
        String imgData = arr[1];
        byte[] bytes = new byte[]{};
        String timestamp = String.valueOf(System.currentTimeMillis());
        String uploadFileName = timestamp + extensionName;
        FastDFSUpUtil fdUtil = new FastDFSUpUtil();
        String url = "";
        try {
            bytes = new BASE64Decoder().decodeBuffer(imgData);
            url = fdUtil.uploadFile(uploadFileName, bytes);
            userApply.setCardPicUrl(url);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return new Result<>().success(userApplyService.save(userApply) ? "添加成功" : "添加失败，请重试");
    }

    /**
     * 申请详情
     */
    @ApiOperation(value = "申请详情")
    @RequiresPermissions("business:user-apply:list")
    @PostMapping("/qryApplyInfoById")
    public UserApplyVo qryApplyInfoById(@RequestBody UserApply userApply)
    {
        UserApplyVo userApplyVo = userApplyService.qryApplyInfoById(userApply);
        //return new Result<>().success(userApplyService.qryApplyInfoById(userApply) ? "添加成功" : "添加失败，请重试");
        return userApplyVo;
    }

}
